﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CodePlex.RoundedButton.Code.Adapters
{
	[Guid("abc48dfc-2b29-4e5d-a311-1268a1053e79")]
	public class ButtonAdapter : System.Web.UI.Adapters.ControlAdapter
	{
		public new Button Control
		{
			get { return (Button)base.Control; }
		}

		protected override void Render(HtmlTextWriter writer)
		{
			Button button = this.Control;

			if (!button.CssClass.Contains("button"))
			{
				base.Render(writer);
				return;
			}

			// begin override html-render
			writer.WriteBeginTag("button");
			writer.WriteAttribute("id", button.ClientID);

			// big or small? (big is default)
			string cssSize = (!button.CssClass.Contains("small") && !button.CssClass.Contains("big"))
				? " big"
				: "";

			if (button.Enabled)
			{
				// button enabled
				writer.WriteAttribute("value", button.Text);
				writer.WriteAttribute("name", button.UniqueID);
				writer.WriteAttribute("type", "submit");
				writer.WriteAttribute("class", string.Format("{0}{1} enabled", button.CssClass, cssSize));

				PostBackOptions options = new PostBackOptions(button);
				options.ValidationGroup = button.ValidationGroup;

				if (button.Page.ClientScript.GetPostBackEventReference(options).Length > 0)
				{
					if (button.CausesValidation)
						options.PerformValidation = true;

					if (button.PostBackUrl.Length > 0)
						options.ActionUrl = button.PostBackUrl;

					// maybe not so important...
					options.ClientSubmit = false;

					// get do_web
					var onClick = button.Page.ClientScript.GetPostBackEventReference(options).Replace("\"", "'");

					if (button.OnClientClick.Length > 0)
					{
						string btnOnclick = button.OnClientClick;
						onClick = string.Format("{0} {{{1};}}", btnOnclick, onClick);
					}

					writer.WriteAttribute("onclick", onClick);
					button.Page.ClientScript.RegisterForEventValidation(options);
				}

				writer.Write(HtmlTextWriter.TagRightChar);
			}
			else
			{
				// button disabled
				writer.WriteAttribute("class", string.Format("{0}{1} disabled", button.CssClass, cssSize));
				writer.WriteAttribute("type", "button");
				writer.WriteAttribute("disabled", "true");
				writer.Write(HtmlTextWriter.TagRightChar);
			}

			writer.WriteBeginTag("span");
			writer.WriteAttribute("class", "additional-background");
			writer.Write(HtmlTextWriter.TagRightChar);

			// add text to the button
			writer.WriteBeginTag("span");
			writer.WriteAttribute("class", "item-text");
			writer.Write(HtmlTextWriter.TagRightChar);
			writer.Write(button.Text);
			writer.WriteEndTag("span");

			// add empty icon-span if cssclss contains icon
			if (button.CssClass.Contains("icon"))
			{
				writer.WriteBeginTag("span");
				writer.WriteAttribute("class", "item-icon");
				writer.Write(HtmlTextWriter.TagRightChar);
				writer.Write("&nbsp;");
				writer.WriteEndTag("span");
			}

			writer.WriteEndTag("span");
			writer.WriteEndTag("button");
		}
	}
}
